#include <gtk/gtk.h>
void func1(GtkButton* button,gpointer user_Data)
{
    printf("hello button:%d\n",*(int*)user_Data);
}
int main(int argc,char* argv)
{
    
    gtk_init(&argc,&argv);
    char* filename[1000];
    int count =0;
    //char* pfile = filename;
    GDir* dir = g_dir_open("../../res/pic",0,NULL);
    const char* pfile;
    while(pfile = g_dir_read_name(dir))
    {
        filename[count] = pfile;
        printf("%s\n",pfile);
        count++;
    }
    // for(int i = 0 ; i < count ; i++)
    // {
    //     printf("%s\n",filename[i]);
    // }
    GtkWidget * window;
    int i = 888;
    int*pi = &i;
    //创建窗口
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    //设置窗口标题
    gtk_window_set_title((GtkWindow*)window,"hello world");
    //设置窗口大小
    gtk_widget_set_size_request(window,800,800);
    gtk_window_set_resizable(window,FALSE);

    // GtkWidget *image = gtk_image_new_from_file("../../res/pic/cat.jpg");
    // GtkWidget *image1 = gtk_image_new_from_file("../../res/pic/cat00.jpg");
    // GtkWidget *image2 = gtk_image_new_from_file("../../res/pic/cat01.jpg");
    // GtkWidget *image3 = gtk_image_new_from_file("../../res/pic/cat02.jpg");
    // GtkWidget *image4 = gtk_image_new_from_file("../../res/pic/cat03.jpg");
    // GtkWidget *image5 = gtk_image_new_from_file("../../res/pic/cat04.jpg");

    //设置垂直布局
    GtkWidget *vbox = gtk_vbox_new(TRUE,5);
    int row = 0;
    if(count % 3 == 0)
    {
        row = count/3;
    }
    else
    {
        row = count/3 + 1;
    }
    
    printf("row=%d\n",row);
    GtkWidget *hboxs[row];
    for(int i = 0 ; i < row ; i++)
    {
        hboxs[i] = gtk_hbox_new(TRUE,5);
    }
    GtkWidget *images[count];
    for(int i = 0 ; i < count ; i++)
    {
        char filepath[100] = "../../res/pic/";
        strcat(filepath,filename[i]);
        printf("%s\n",filepath);
        images[i] = gtk_image_new_from_file(filepath);
        printf("%d\n",i/3);
        gtk_container_add((GtkContainer*)hboxs[i/3],images[i]);
    }
    // gtk_container_add((GtkContainer*)hboxs[0],images[0]);
    for(int i = 0 ; i < row ; i++)
    {
        gtk_container_add((GtkContainer*) vbox,hboxs[i]);
    }

    // GtkWidget *hbox1 = gtk_hbox_new(TRUE,0);
    // gtk_container_add((GtkContainer*) hbox1,image);
    // gtk_container_add((GtkContainer*) hbox1,image1);
    // gtk_container_add((GtkContainer*) hbox1,image2);
    // GtkWidget *hbox2 = gtk_hbox_new(TRUE,0);
    // gtk_container_add((GtkContainer*)hbox2,image3);
    // gtk_container_add((GtkContainer*)hbox2,image4);


    // GtkWidget *button = gtk_button_new_with_label("按钮");
    // g_signal_connect(button,"clicked",G_CALLBACK(func1),pi);
    g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);

    // gtk_container_add((GtkContainer*)vbox,hbox1);
    // gtk_container_add((GtkContainer*)vbox,hbox2);
    gtk_container_add((GtkContainer*)window,vbox);
    // const char *p = gtk_button_get_label((GtkButton*) button);
    // printf("%s\n",p);
    // printf("hello\n");
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}